package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Slf4j
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    /**
     * 查询所有角色
     *
     * @return
     */
    @Override
    public List<Role> findAll() {

        return roleDao.findAll();
    }

    @Override
    public List<Role> findAllRoleByUserId(Integer UserId) {
        List<Role> roles = roleDao.selectByUserId(UserId);
        for (Role role : roles) {
            log.info("[角色为]role:{}", role);
        }
        return roles;
    }
        @Override
        public PageResult pageQuery (QueryPageBean queryPageBean){
            log.info("[分页查询]data:{}", queryPageBean);
            //设置分页查询参数
            PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
            //查询page
            Page<Role> page = roleDao.selectByCondition(queryPageBean.getQueryString());
            return new PageResult(page.getTotal(), page.getResult());
        }

        @Transactional
        @Override
        public void add (Role role, Integer[]permissionIds, Integer[]menuIds){
            log.info("[信息添加]role:{},permissionIds:{},menuIds:{}", role, permissionIds, menuIds);
            roleDao.insertRole(role);
            for (Integer permissionId : permissionIds) {
                roleDao.insertRoleAndPermission(role.getId(), permissionId);
            }
            for (Integer menuId : menuIds) {
                roleDao.insertRoleAndmenu(role.getId(), menuId);
            }
        }

        @Override
        public Role findById (Integer id){
            return roleDao.findById(id);

        }

        @Override
        public List<Integer> RoleAndPermission (Integer id){
            return roleDao.RoleAndPermission(id);

        }

        @Override
        public List<Integer> RoleAndMenu (Integer id){
            return roleDao.RoleAndMenu(id);

        }

        @Override
        @Transactional
        public void update (Role role, Integer[]permissionId, Integer[]menuIds){
            roleDao.deletepermission(role);
            roleDao.deletemenu(role);
            for (Integer permission : permissionId) {
                roleDao.insertRoleAndPermission(role.getId(), permission);
            }
            for (Integer menuId : menuIds) {
                roleDao.insertRoleAndmenu(role.getId(), menuId);
            }
            roleDao.update(role);

        }

        @Override
        @Transactional
        public void delete (Integer id){
            long count1 = roleDao.findPermissionbyId(id);
            long count2 = roleDao.findMenubyId(id);

            if (count1 > 0 || count2 > 0) {
                throw new BusinessRuntimeException("该删除项有关联项,不能删除!");
            }
            roleDao.deleteRoleAddMenu(id);
            roleDao.deleteRoleAddPermission(id);
            roleDao.deleteById(id);
        }

}

